// 教材订购系统实现（C/C++ + MySQL）
// 使用C++开发语言，连接MySQL数据库

#include <iostream>
#include <mysql/mysql.h>
#include <string>
#include <iomanip>

using namespace std;

// 数据库连接配置
const string DB_HOST = "localhost";
const string DB_USER = "root";
const string DB_PASS = "password";
const string DB_NAME = "textbook_order";

MYSQL *connectDB() {
    MYSQL *conn = mysql_init(nullptr);
    if (conn == nullptr) {
        cerr << "MySQL 初始化失败!" << endl;
        exit(EXIT_FAILURE);
    }

    // 连接数据库
    if (mysql_real_connect(conn, DB_HOST.c_str(), DB_USER.c_str(), DB_PASS.c_str(), DB_NAME.c_str(), 3306, nullptr, 0) == nullptr) {
        cerr << "数据库连接失败: " << mysql_error(conn) << endl;
        mysql_close(conn);
        exit(EXIT_FAILURE);
    }

    return conn;
}

// 显示教材信息维护界面
void displayBookInterface() {
    cout << "\n------------------------------" << endl;
    cout << " 教材信息维护界面 " << endl;
    cout << "------------------------------" << endl;
    cout << " 教材名称: [__________________]" << endl;
    cout << " 作者:     [__________________]" << endl;
    cout << " ISBN:     [__________________]" << endl;
    cout << " 类别:     [ 下拉菜单: 选择类别 ]" << endl;
    cout << " [保存]  [浏览]  [清空]" << endl;
    cout << "------------------------------" << endl;
}

// 显示浏览区
void displayBrowseSection(MYSQL *conn) {
    cout << "\n------------------------------" << endl;
    cout << " 浏览区 " << endl;
    cout << "------------------------------" << endl;

    string query = "SELECT * FROM books";
    if (mysql_query(conn, query.c_str())) {
        cerr << "查询教材信息失败: " << mysql_error(conn) << endl;
        return;
    }

    MYSQL_RES *result = mysql_store_result(conn);
    if (result == nullptr) {
        cerr << "获取查询结果失败: " << mysql_error(conn) << endl;
        return;
    }

    MYSQL_ROW row;
    while ((row = mysql_fetch_row(result))) {
        cout << " 记录: " << row[0] << " | " << row[1] << " | " << row[2] << " | " << row[3] << endl;
    }

    mysql_free_result(result);
    cout << "------------------------------" << endl;
}

// 教材信息维护 - 插入新教材信息
void insertBook(MYSQL *conn) {
    string bookID, title, author, isbn, category;
    cout << "输入教材编号: ";
    cin >> bookID;
    cout << "输入教材标题: ";
    cin.ignore();
    getline(cin, title);
    cout << "输入作者: ";
    getline(cin, author);
    cout << "输入ISBN: ";
    getline(cin, isbn);
    cout << "输入类别: ";
    getline(cin, category);

    string query = "INSERT INTO books (book_id, title, author, isbn, category) VALUES ('" + bookID + "', '" + title + "', '" + author + "', '" + isbn + "', '" + category + "')";

    if (mysql_query(conn, query.c_str())) {
        cerr << "插入数据失败: " << mysql_error(conn) << endl;
    } else {
        cout << "成功插入教材信息!" << endl;
    }
}

// 订单生成 - 事务处理
void generateOrder(MYSQL *conn) {
    string orderID, studentID, bookID, quantity;
    cout << "输入订单编号: ";
    cin >> orderID;
    cout << "输入学生编号: ";
    cin >> studentID;
    cout << "输入教材编号: ";
    cin >> bookID;
    cout << "输入订购数量: ";
    cin >> quantity;

    // 开启事务
    mysql_autocommit(conn, 0);

    string orderQuery = "INSERT INTO orders (order_id, student_id) VALUES ('" + orderID + "', '" + studentID + "')";
    string detailQuery = "INSERT INTO order_details (order_id, book_id, quantity) VALUES ('" + orderID + "', '" + bookID + "', " + quantity + ")";
    string updateStockQuery = "UPDATE books SET stock = stock - " + quantity + " WHERE book_id = '" + bookID + "'";

    if (mysql_query(conn, orderQuery.c_str()) || mysql_query(conn, detailQuery.c_str()) || mysql_query(conn, updateStockQuery.c_str())) {
        cerr << "订单生成失败，回滚事务: " << mysql_error(conn) << endl;
        mysql_rollback(conn);
    } else {
        mysql_commit(conn);
        cout << "订单生成成功!" << endl;
    }

    // 恢复自动提交模式
    mysql_autocommit(conn, 1);
}

int main() {
    MYSQL *conn = connectDB();

    int choice;
    do {
        cout << "\n教材订购系统菜单:\n";
        cout << "1. 显示教材信息维护界面\n";
        cout << "2. 插入教材信息\n";
        cout << "3. 浏览教材信息\n";
        cout << "4. 生成订单\n";
        cout << "0. 退出\n";
        cout << "请输入操作编号: ";
        cin >> choice;

        switch (choice) {
            case 1:
                displayBookInterface();
                break;
            case 2:
                insertBook(conn);
                break;
            case 3:
                displayBrowseSection(conn);
                break;
            case 4:
                generateOrder(conn);
                break;
            case 0:
                cout << "退出系统." << endl;
                break;
            default:
                cout << "无效输入，请重试." << endl;
        }
    } while (choice != 0);

    mysql_close(conn);
    return 0;
}

